package com.rebate.web.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rebate.model.RebateOrder;
import com.rebate.service.IBaseService;
import com.rebate.util.Page;
import com.rebate.util.PageUtils;

@Controller
public class RebateOrderServlet extends BaseServlet{
	
	private String view = "/order/listorder";
	private String editView = "/order/editorder";
	@Autowired
	private IBaseService<RebateOrder> rebateOrderService;
	
	/**
	 * 返利订单列表页面
	 * @throws IOException 
	 */
	@RequestMapping(value = "/order/index.htm")
	public String index(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateOrder") RebateOrder rebateOrder) throws IOException {
		return view;
	}
	
	
	/**
	 * 编辑返利订单页面初始化
	 * @throws IOException 
	 */
	@RequestMapping(value = "/order/edit.htm")
	public String editInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateOrder") RebateOrder rebateOrder) throws IOException {
		rebateOrder = rebateOrderService.findById(rebateOrder.getId());
		model.put("rebateOrder", rebateOrder);
		return editView;
	}
	
	/**
	 * 更新返利订单
	 * @throws IOException 
	 */
	@RequestMapping(value = "/order/update.htm")
	public void update(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateOrder") RebateOrder rebateOrder) throws IOException {
		if(rebateOrderService.update(rebateOrder)) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 查询返利订单
	 * @throws IOException 
	 */
	@RequestMapping(value = "/order/list.htm")
	public void getList(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateOrder") RebateOrder rebateOrder) throws IOException {
		Page<RebateOrder> page = PageUtils.getPage(request);
		page = rebateOrderService.queryForListPage(page, rebateOrder);
		print(request, response, page);
	}
	
}
